<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.lp.cfg.ProConfig"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
	<%@ include file="/WEB-INF/wechat/iot/common/resource_lib.jsp"%>
	<link type="text/css" href="<%=basePath%>/lib/pintuer/pintuer.min.css" rel="stylesheet"/>
	<script type="text/javascript" src="../../lib/layer/layer.js"></script>

	<style type="text/css">
		.btn-span{
			border:1px solid #3499da; 
		    padding: 1px 10px;
		    color:#3499da ;
		    margin:0 10px;
		    border-radius: 4px;
		    cursor: pointer;
		    display: inline-block;
		}
		.self-input{
		}
		.self-sapn{
			display: inline-block;
			width: 40%;
			text-overflow: ellipsis;
			white-space: nowrap;overflow: hidden;
			
		}
		.float-left.width-50{
			margin-bottom: 8px;
		}
		.input-small{
			height: 36px;
			line-height: 36px;
		}
	</style>
</head>
<body>
	<div class="t-c title-info" >
		<span>Device parameter configuration</span>
		<div class="back" onclick="returnBack()" style=" position: fixed;top: 0px;" >
		Back</div>
	</div>
	<div style="height: 50px;"></div>
	<div id="container" style="padding: 10px 5px;">
		
	</div>
	<div id="template" class="hide">
		<div class="panel  mt-10" >
			<div class="panel-head " style="background-color:#f5f5f5;">
				<strong>{name}</strong>
		
				<span style="float: right;" class="pr-10">Time:<span>[mtime]</span></span>
			</div>
			<div class="panel-body clear">
				{body}				
			</div>
			<div class="panel-foot clear" style="background-color:#fff;">
				<span class="float-right btn-span" onclick="downparam({id})" >Parameter delivery</span>
			</div>
		</div>
	</div>
	
	<div id="template3" class="hide">
		<div class="" style="padding:5px 0;">
			<span class="self-sapn">{0}：</span>
			<select class="input input-small input-select self-input params">
				{1}
			</select>
		</div>
	</div>
	
	<div id="template2" class="hide">
		<div class="" style="position: relative;">
			<input class="input input-small self-input {5}-value" readonly="readonly"  tag="{4}" value="{1}" >
			<!-- <input class="input input-small self-input params" placeholder="数据下发" tag="{4}" style=" margin-top: 10px;"  > -->
			<span  style="position: absolute;top:9px;right: 10px;z-index: 1000;" >{2}</span>
			<p style="padding-top: 10px;margin:0;">
				Note: {3}
			</p>
		</div>
	</div>
	
</body>

<script type="text/javascript">

	// 参数读取
	function sensor_param_read( id ,obj){
		commonAjax('PUT', localUrl+'/sensor/param/read.json' , {id:id},function(data){
			if(isOK(data)){
				tip("Read command successfully delivered, please wait..."); 
				setTimeout(function() {
					getSensorSetting(GetQueryString("id"));
				}, 3000);
			}else{
				tip(data.statusMsg,'e') ;
			}
		});
	}
	
	/**
	 * 数据下发
	 */
	function downparam(id){
		layer.prompt({title: 'Data delivery', formType: 3}, function(val, index){
			commonAjax('PUT',localUrl+'/sensor/param/down.json', {sdata : val  ,id:id  },function(data){
				if(isOK(data)){
					tip("Write command successfully, please wait...");  
				}else{
					$.toptip(data.statusMsg, 'error');
				}	
			});			
			layer.close(index);
		});
		return false;
	}
	
	function getDisInfo(data){
		var t =eval('('+ gdv( data.infos,'{}') +')');
		if( t!= null && t.readType == 1){
			return "" ;
		}else{
			return "hide" ;
		}
	}
	
	// 参数下发
	function sensor_param_down( id, obj ){
		var inputs = $(obj).parent().parent().find('input.params,select.params');
		var str_sdata = '' ;
		for(var i=0;i<inputs.length;i++){
			str_sdata += ","+inputs.eq(i).val();
		}
		var downparam =str_sdata.substring(1) ;
		
		if( inputs.eq(0).attr("tag") !=null && inputs.eq(0).attr("tag") != '' ){
			var info_data = eval ("("+  inputs.eq(0).attr("tag")  + ")") ;
			if(info_data.max != null && info_data.max != '' ){
				if( downparam >= info_data.max ){
					tip("Parameter greater than maximum",'e');
					return ;
				}	
			}
			
			if(info_data.min != null && info_data.min != '' ){
				if( downparam <= info_data.min ){
					tip("Parameter less than maximum",'e');
					return ;
				}	
			}
		}
		
		if( $(obj).prev().prev().html() == 'Text message delivery' ){
			downparam = $URL.encode(str_sdata.substring(1)) ;
		}
		if(inputs.length >0){
			commonAjax('PUT',localUrl+'/sensor/param/down.json', {sdata: downparam ,id:id  },function(data){
				if(isOK(data)){
					tip("Write command successfully, please wait...");  
					setTimeout(function() {
						udpSensorSetting(GetQueryString("id"));
						inputs.eq(0).val('') ;
					}, 2500);
				}else{
					tip(data.statusMsg,'e') ;
				}	
			});			
		}else{
			tip("No command has been delivered",'e');  
		}
	}
	
	setInterval(function(){
		udpSensorSetting(GetQueryString("id"));
	}, 10*1000);
	
	// 更新数据
	function udpSensorSetting(nid){
		postAjax(localUrl+"/page/sensor.json?pageSize=300&paged=1", { node_id : nid , data_type:1 },function(data){
			if(isOK(data)){
				var dataT = data.data.data ;
				for(var i=0 ; i<dataT.length; i++){
					$("."+dataT[i].id + "-value").val( dataT[i].sdata ) ;
				}
			}
		});
	}
	
	getSensorSetting(GetQueryString("id"));

	function getSensorSetting(nid){
		postAjax(localUrl+"/page/sensor.json?pageSize=300&paged=1", { node_id : nid , data_type:1 },function(data){
			if(isOK(data)){
				var dataT = data.data.data ;
				$("#container").empty();
				for(var i=0 ; i<dataT.length; i++){
					dataT[i].body = getBodyContent( dataT[i].name , dataT[i].sdata, dataT[i].param_config , dataT[i].param_names , dataT[i].infos.replace(/"/g,"'") , dataT[i].id );
					$("#container").append( $("#template").html().format(dataT[i]) );
				}
			}else{
				$("#container").append( "<p style='color:#666;text-align:center;'>No parameter configuration is available</p>");
			}
		});
	}
	
	function getBodyContent(as,bs,cs,d,f , g){
		var s = '' ;
		s += $("#template2").html().format( as ,bs, cs ,d,f , g);
		return s;
	}
	
	function selectInit(a,b,c){
		var s = '' ;
		for(var i=0;i<a.length;i++){
			s += "<option "+ tom(b[i]==c,"selected='selected'","") +"  value='"+b[i]  +"'>"+ a[i] +"</option>";
		}
		return s ;
	}
	
	function returnBack(){
		forward( '/wiot/device?id=' + GetQueryString("sid") );
	}
	
</script>

</html>